package com.egg.ylt.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;


import com.egg.ylt.R;
import com.egg.ylt.pojo.MemberCardListEntity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by sichaoqi on 2018/10/9.
 */

public class ADA_TimeCardRecycler extends RecyclerView.Adapter<ADA_TimeCardRecycler.TimeCardViewHolder> {

    private Context mContext;
    private LayoutInflater mInflater;
    private List<MemberCardListEntity.ListBean> mList;
    private OnItemClickListener mItemListener;

    public ADA_TimeCardRecycler(Context context) {
        this.mContext = context;
        mInflater = LayoutInflater.from(mContext);
        mList = new ArrayList<>();
    }

    public void setList(List<MemberCardListEntity.ListBean> list) {
        if (mList != null) {
            mList.clear();
        }
        mList.addAll(list);
        notifyDataSetChanged();
    }

    @Override
    public TimeCardViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new TimeCardViewHolder(mInflater.inflate(R.layout.item_time_card_recycler, parent, false));
    }

    @Override
    public void onBindViewHolder(TimeCardViewHolder holder, int position) {
        final MemberCardListEntity.ListBean entity = mList.get(position);
        //次卡名称
        holder.cardTvGoodsName.setText(entity.getName());
        //有效期
        holder.cardTvEndDate.setText("有效期：" + entity.getEndDate());
        //次数
        holder.cardTvTimes.setText(entity.getTimes() + "/" + entity.getTime() + " 次");
        //所属门店
        holder.cardTvShop.setText(entity.getShopId());

//        holder.itemView.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                mItemListener.onItemClick(entity.getId());
//            }
//        });
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    static class TimeCardViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.card_tv_goodsName)
        TextView cardTvGoodsName;
        @BindView(R.id.card_tv_endDate)
        TextView cardTvEndDate;
        @BindView(R.id.card_tv_times)
        TextView cardTvTimes;
        @BindView(R.id.card_tv_name)
        TextView cardTvName;
        @BindView(R.id.card_tv_shop)
        TextView cardTvShop;

        public TimeCardViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface OnItemClickListener {
        void onItemClick(String id);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mItemListener = listener;
    }
}
